<template>
  <div class="preview">
    <div class="top">
      <img v-for="(src, index) in data"
           v-lazy="src"
           :class="{active: index === activeIndex}"
           @click="previewClick(index)"/>
    </div>
    <div v-if="activeIndex !== -1"
         class="bottom">
      <div class="view">
        <img :src="data[activeIndex]"/>
        <div class="close"
             @click="activeIndex = -1">
          ×
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts"
        setup>
import {ref} from 'vue'

defineProps<{
  data: string[]
}>()

let activeIndex = ref<number>(-1)

function previewClick(index: number): void {
  activeIndex.value = activeIndex.value === index ? -1 : index
}
</script>

<style lang="less"
       scoped>
.preview {
  .top {
    display: flex;
    flex-wrap: wrap;

    img {
      width: 120px;
      height: 120px;
      object-fit: contain;
      border: 1px solid #e4e4e4;
      cursor: pointer;
      margin-left: 20px;
      margin-bottom: 10px;

      &.active {
        border: 1px solid @xtxColor;
      }
    }
  }

  .bottom {
    display: flex;
    justify-content: center;

    .view {
      width: 680px;
      height: 400px;
      display: flex;
      position: relative;
      background-color: #f8f8f8;
      border: 1px solid #e4e4e4;
      justify-content: center;
      align-items: center;

      img {
        width: 600px;
        height: 380px;
        object-fit: contain;
      }

      .close {
        position: absolute;
        top: 0;
        right: 0;
        width: 30px;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #aaa;
        font-size: 24px;
        cursor: pointer;
        color: white;
      }
    }
  }
}
</style>